package com.example.ross.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * @version 1.0.0
 * @className: Result
 * @description: 返回结果，公共类
 * @author: zhangjunfa
 * @date: 2023/5/6 14:52
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 返回结果
     */
    private T data;

    private Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        Result<T> res = new Result<>(20001, "成功");
        res.setData(data);
        return res;
    }

    public static <T> Result<T> success() {
        return new Result<>(20001, "成功");
    }

    public static <T> Result<T> error() {
        return new Result<>(40001, "失败");
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(40001, msg);
    }

    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<>(code, msg);
    }

}
